import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiFormTagApi } from '#/api/ai/form/formTag';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { IconifyIcon } from '@vben/icons';

import { ColorPicker } from '#/components/color-picker';
import { DictBadge } from '#/components/dict-badge';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '标签名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入标签名称',
      },
    },
    {
      fieldName: 'icon',
      label: '标签图标',
      component: 'IconPicker',
      componentProps: {
        placeholder: '请选择分类图标',
        prefix: 'carbon',
      },
    },
    {
      fieldName: 'color',
      label: '颜色',
      component: 'ColorPicker',
      componentProps: {
        placeholder: '请输入颜色',
        showAlpha: false,
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      rules: 'required',
      defaultValue: true,
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'templatesCount',
      label: '表单个数',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入表单个数',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '标签名称',
      field: 'name',
    },
    {
      label: '标签图标',
      field: 'icon',
      render: (val, data) => {
        return val
          ? h(IconifyIcon, {
              icon: val,
              color: data?.color,
            })
          : '无';
      },
    },
    {
      label: '颜色',
      field: 'color',
      render: (val) => {
        return val
          ? h(ColorPicker, {
              modelValue: val,
              disabled: true,
            })
          : '无';
      },
    },
    {
      label: '排序',
      field: 'sort',
    },
    {
      label: '系统内置',
      field: 'systemFlag',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '表单个数',
      field: 'templatesCount',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '标签名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入标签名称',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'Select',
      componentProps: {
        allowClear: true,
        placeholder: '请选择是否系统内置',
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
      },
    },
    {
      fieldName: 'templatesCount',
      label: '表单个数',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入表单个数',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AiFormTagApi.FormTag>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '表单标签ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'name',
      title: '标签名称',
      minWidth: 120,
    },
    {
      field: 'icon',
      title: '标签图标',
      minWidth: 120,
      slots: { default: 'icon' },
    },
    {
      field: 'color',
      title: '颜色',
      minWidth: 120,
      slots: { default: 'color' },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 120,
    },
    {
      field: 'systemFlag',
      title: '是否系统内置',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'templatesCount',
      title: '表单个数',
      minWidth: 120,
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
